//
//  HBBaseTabBar.m
//  HeartBeat
//
//  Created by karong on 2023/8/23.
//

#import "HBBaseTabBar.h"

@implementation HBBaseTabBar
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.translucent = YES;
        self.backgroundColor = UIColor.font_white_ffffff;
        [self setShadowImage:[[UIImage alloc]init]];
        [self setBackgroundImage:[[UIImage alloc]init]];
        _oldSafeAreaInsets = UIEdgeInsetsZero;
    }
    return self;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if([keyPath isEqualToString:@"frame"]) {
        CGRect newFrame = [change[NSKeyValueChangeNewKey] CGRectValue];
        if (newFrame.size.height == 33) {
            newFrame.size.height = 48;
            ((UIView *)object).frame = newFrame;
        }
    }
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    NSArray *items = self.items;
    NSInteger index = 0;
    for (UITabBarItem *item in items) {
        if (@available(iOS 13.0, *)) {
            self.tintColor = UIColor.font_yellow_FFCC00;
            [self setUnselectedItemTintColor:UIColor.font_black_1A1A1A];
        } else {
            NSDictionary *normalAttrDict = @{NSForegroundColorAttributeName:UIColor.font_black_1A1A1A, NSFontAttributeName: [UIFont systemFontOfSize:11]};
            NSDictionary *selectedAttrDict = @{NSForegroundColorAttributeName:UIColor.font_yellow_FFCC00, NSFontAttributeName: [UIFont systemFontOfSize:11]};
            [item setTitleTextAttributes:normalAttrDict forState:UIControlStateNormal];
            [item setTitleTextAttributes:selectedAttrDict forState:UIControlStateSelected];
        }
        item.imageInsets = UIEdgeInsetsMake(0, 0, 0, 0);
        [item setTitlePositionAdjustment:UIOffsetMake(0, -2)];
        if (index == 0) {
            item.image = [IMG(@"teb_zhuishu_n") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            item.selectedImage = [IMG(@"teb_zhuishu_p") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            item.title = WSLocalized(@"追書");
        }
        else if (index == 1) {
            item.image = [IMG(@"teb_faxian_n") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            item.selectedImage = [IMG(@"teb_faxian_p") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            item.title = WSLocalized(@"發現");
        }
        else if (index == 2) {
            item.image = [IMG(@"teb_me_n") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            item.selectedImage = [IMG(@"teb_me_p") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            item.title = WSLocalized(@"我的");
        }
        index ++;
    }
}
@end
